/*
 * Copyright 2011-2012 Rivoli team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.rivoli.configuration

import com.googlecode.rivoli.RivoliException

class Configuration {
	private Map<String, Configurable> configurables

	Configuration() {
		configurables = [:]
	}

	void addConfigurable(String name, Configurable configurable) throws RivoliException {
		if (configurables.containsKey(name)) {
			throw new RivoliException("configuration already contains a configurable named '${name}'")
		}

		configurables[name] = configurable
	}

	Configurable getConfigurable(String name) throws RivoliException {
		if (!configurables.containsKey(name)) {
			throw new RivoliException("'${name}' does not exist")
		}

		configurables[name]
	}

	// the Groovy compiler needs /something/ before the <T extends ...> - strictfp does not do any harm
	strictfp <T extends Configurable> Map<String, T> getConfigurables(Class<T> type) {
		configurables.findAll {String name, Configurable configurable ->
			configurable in type
		}
	}
}